What is wrong with this code for reading binary files? [on hold]
Posted
by
qed
on Programmers
See other posts from Programmers
or by qed
Published on 2013-11-06T18:10:50Z
Indexed on
2013/11/06
22:07 UTC
Read the original article
Hit count: 88
What is wrong with this code for reading binary files? It compiles OK, but will not print out the file as planned, in fact, it prints nothing at all.
#include <iostream>
#include <fstream>
int main(int argc, const char *argv[])
{
if (argc < 2) {
::std::cerr << "usage: " << argv[0] << " <filename>\n";
return 1;
}
::std::basic_ifstream<unsigned char> in(argv[1], ::std::ios::binary);
unsigned char uc;
while (in.get(uc)) {
printf("%02X ", uc);
}
// TODO: error handling, in case the file could not be opened or read
return 0;
}
© Programmers or respective owner